<!--
 * @Descripttion: (bom信息/prod_bom)
 * @Author: (admin)
 * @Date: (2025-05-15)
-->
<template>
	<div>
		<el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
			<el-form-item label="产品编号" prop="itemNo">
				<el-input v-model="queryParams.itemNo" placeholder="请输入产品编号" />
			</el-form-item>
			<el-form-item label="零件号" prop="partCode">
				<el-input v-model="queryParams.partCode" placeholder="请输入零件号" />
			</el-form-item>
			<el-form-item label="是否填充" prop="isFill">
				<el-select clearable v-model="queryParams.isFill" placeholder="请选择是否填充">
					<el-option v-for="item in options.bom_is_fill" :key="item.dictValue" :label="item.dictLabel"
						:value="item.dictValue">
						<span class="fl">{{ item.dictLabel }}</span>
						<span class="fr" style="color: var(--el-text-color-secondary);">{{ item.dictValue }}</span>
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="DMS订单" prop="dmsOrderNo">
				<el-input v-model="queryParams.dmsOrderNo" placeholder="请输入DMS订单" />
			</el-form-item>
			<el-form-item label="计划号" prop="planNo">
				<el-input v-model="queryParams.planNo" placeholder="请输入计划号" />
			</el-form-item>
			<el-form-item>
				<el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
				<el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
			</el-form-item>
		</el-form>
		<!-- 工具区域 -->
		<el-row :gutter="15" class="mb10">
			<el-col :span="1.5">
				<el-button type="primary" v-hasPermi="['prod:bom:add']" plain icon="plus" @click="handleAdd">
					{{ $t('btn.add') }}
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" :disabled="single" v-hasPermi="['prod:bom:edit']" plain icon="edit"
					@click="handleUpdate">
					{{ $t('btn.edit') }}
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" :disabled="multiple" v-hasPermi="['prod:bom:delete']" plain icon="delete"
					@click="handleDelete">
					{{ $t('btn.delete') }}
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-dropdown trigger="click" v-hasPermi="['prod:bom:import']">
					<el-button type="primary" plain icon="Upload">
						{{ $t('btn.import') }}<el-icon class="el-icon--right"><arrow-down /></el-icon>
					</el-button>
					<template #dropdown>
						<el-dropdown-menu>
							<el-dropdown-item command="upload">
								<importData templateUrl="ProdManage/ProdBom/importTemplate" importUrl="/ProdManage/ProdBom/importData"
									@success="handleFileSuccess"></importData>
							</el-dropdown-item>
						</el-dropdown-menu>
					</template>
				</el-dropdown>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="download" @click="handleExport" v-hasPermi="['prod:bom:export']">
					{{ $t('btn.export') }}
				</el-button>
			</el-col>
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
		</el-row>

		<el-table :data="dataList" v-loading="loading" ref="table" border header-cell-class-name="el-table-header-cell"
			highlight-current-row @sort-change="sortChange" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="50" align="center" />
			<el-table-column prop="bomId" label="bom主键" fixed="left" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('bomId')" />
			<el-table-column prop="dmsOrderNo" label="DMS订单" fixed="left" width="150" align="center"
				:show-overflow-tooltip="true" v-if="columns.showColumn('dmsOrderNo')" />
			<el-table-column prop="planNo" label="计划号" fixed="left" width="180" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('planNo')" />
			<el-table-column prop="itemNo" label="产品编号" width="150" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('itemNo')" />
			<el-table-column prop="itemType" label="产品类型" width="120" align="center" v-if="columns.showColumn('itemType')">
				<template #default="scope">
					<dict-tag :options="options.product_type" :value="scope.row.itemType" />
				</template>
			</el-table-column>
			<el-table-column prop="partCode" label="零件号" width="150" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('partCode')" />
			<el-table-column prop="qty" label="用量" width="100" align="center" v-if="columns.showColumn('qty')" />
			<el-table-column prop="effectDate" label="生效时间" width="160" :show-overflow-tooltip="true"
				v-if="columns.showColumn('effectDate')" />
			<el-table-column prop="expirationDate" label="失效时间" width="160" :show-overflow-tooltip="true"
				v-if="columns.showColumn('expirationDate')" />
			<el-table-column prop="changeNo" label="变更编号" width="120" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('changeNo')" />
			<el-table-column prop="supplierCode" label="供应商" width="120" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('supplierCode')" />
			<el-table-column prop="wareCode" label="仓库" width="120" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('wareCode')" />
			<el-table-column prop="isFill" label="是否填充" width="120" align="center" v-if="columns.showColumn('isFill')">
				<template #default="scope">
					<dict-tag :options="options.bom_is_fill" :value="scope.row.isFill" />
				</template>
			</el-table-column>
			<el-table-column prop="obtainType" label="获取类型" width="120" align="center"
				v-if="columns.showColumn('obtainType')">
				<template #default="scope">
					<dict-tag :options="options.bom_obtain_type" :value="scope.row.obtainType" />
				</template>
			</el-table-column>
			<el-table-column prop="createUser" label="管理员" width="120" align="center" v-if="columns.showColumn('createUser')">
				<template #default="scope">
					<dict-tag :options="options.bom_is_fill" :value="scope.row.createUser" />
				</template>
			</el-table-column>
			<el-table-column prop="remark" label="备注" width="120" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('remark')" />
			<el-table-column label="操作" width="160" fixed="right">
				<template #default="scope">
					<el-button-group>
						<el-button text v-hasPermi="['prod:bom:edit']" type="success" icon="edit" @click="handleUpdate(scope.row)">
							{{ $t('btn.edit') }}
						</el-button>
						<el-button text v-hasPermi="['prod:bom:delete']" type="danger" icon="delete"
							@click="handleDelete(scope.row)">
							{{ $t('btn.delete') }}
						</el-button>
					</el-button-group>
				</template>
			</el-table-column>
		</el-table>
		<pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
			@pagination="getList" />


		<el-dialog draggable :close-on-click-modal="false" :title="title" :lock-scroll="false" v-model="open">
			<el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
				<el-row :gutter="20">
					<!-- <el-col :lg="12">
						<el-form-item label="bom主键" prop="bomId">
							<el-input v-model="form.bomId" placeholder="请输入bom主键" :disabled="opertype != 1" />
						</el-form-item>
					</el-col> -->
					<el-col :lg="12">
						<el-form-item label="DMS订单" prop="dmsOrderNo">
							<el-input v-model="form.dmsOrderNo" placeholder="请输入DMS订单" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="计划号" prop="planNo">
							<el-input v-model="form.planNo" placeholder="请输入计划号" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="产品编号" prop="itemNo">
							<el-input v-model="form.itemNo" placeholder="请输入产品编号" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="产品类型" prop="itemType">
							<el-select v-model="form.itemType" placeholder="请选择产品类型" style="width: 100%;">
								<el-option v-for="item in options.product_type" :key="item.dictValue" :label="item.dictLabel"
									:value="item.dictValue"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="零件号" prop="partCode">
							<el-input v-model="form.partCode" placeholder="请输入零件号" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="用量" prop="qty">
							<el-input v-model="form.qty" placeholder="请输入用量" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="生效时间" prop="effectDate">
							<el-date-picker v-model="form.effectDate" type="datetime" placeholder="选择日期时间"
								value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%;">
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="失效时间" prop="expirationDate">
							<el-date-picker v-model="form.expirationDate" type="datetime" placeholder="选择日期时间"
								value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%;">
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="变更编号" prop="changeNo">
							<el-input v-model="form.changeNo" placeholder="请输入变更编号" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="供应商" prop="supplierCode">
							<el-input v-model="form.supplierCode" placeholder="请输入供应商" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="仓库" prop="wareCode">
							<el-input v-model="form.wareCode" placeholder="请输入仓库" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="是否填充" prop="isFill">
							<el-select v-model="form.isFill" placeholder="请选择是否填充" style="width: 100%;">
								<el-option v-for="item in options.bom_is_fill" :key="item.dictValue" :label="item.dictLabel"
									:value="item.dictValue"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="获取类型" prop="obtainType">
							<el-select v-model="form.obtainType" placeholder="请选择获取类型" style="width: 100%;">
								<el-option v-for="item in options.bom_obtain_type" :key="item.dictValue" :label="item.dictLabel"
									:value="item.dictValue"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="管理员" prop="createUser">
							<el-select v-model="form.createUser" placeholder="请选择管理员" style="width: 100%;">
								<el-option v-for="item in options.bom_is_fill" :key="item.dictValue" :label="item.dictLabel"
									:value="item.dictValue"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :lg="24">
						<el-form-item label="备注" prop="remark">
							<el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer v-if="opertype != 3">
				<el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
				<el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="prodbom">
import {
	pageProdBom,
	addProdBom, delProdBom,
	updateProdBom, getProdBom,
}
	from '@/api/prodManage/prodBom.js'
import importData from '@/components/ImportData'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
	pageNum: 1,
	pageSize: 10,
	sort: '',
	sortType: 'asc',
	itemNo: undefined,
	partCode: undefined,
	isFill: undefined,
	dmsOrderNo: undefined,
	planNo: undefined,
})
const columns = ref([
	{ visible: false, prop: 'bomId', label: 'bom主键' },
	{ visible: true, prop: 'dmsOrderNo', label: 'DMS订单' },
	{ visible: true, prop: 'planNo', label: '计划号' },
	{ visible: true, prop: 'itemNo', label: '产品编号' },
	{ visible: true, prop: 'itemType', label: '产品类型' },
	{ visible: true, prop: 'partCode', label: '零件号' },
	{ visible: true, prop: 'qty', label: '用量' },
	{ visible: true, prop: 'effectDate', label: '生效时间' },
	{ visible: true, prop: 'expirationDate', label: '失效时间' },
	{ visible: true, prop: 'changeNo', label: '变更编号' },
	{ visible: true, prop: 'supplierCode', label: '供应商' },
	{ visible: true, prop: 'wareCode', label: '仓库' },
	{ visible: true, prop: 'isFill', label: '是否填充' },
	{ visible: true, prop: 'obtainType', label: '获取类型' },
	{ visible: true, prop: 'createUser', label: '管理员' },
	{ visible: false, prop: 'remark', label: '备注' },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
	"product_type",
	"bom_is_fill",
	"bom_obtain_type",
]

proxy.getDicts(dictParams).then((response) => {
	response.data.forEach((element) => {
		state.options[element.dictType] = element.list
	})
})

function getList() {
	loading.value = true
	pageProdBom(queryParams).then(res => {
		const { code, data } = res
		if (code == 200) {
			dataList.value = data.result
			total.value = data.totalNum
			loading.value = false
		}
	})
}

// 查询
function handleQuery() {
	queryParams.pageNum = 1
	getList()
}

// 重置查询操作
function resetQuery() {
	proxy.resetForm("queryRef")
	handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
	ids.value = selection.map((item) => item.bomId);
	single.value = selection.length != 1
	multiple.value = !selection.length;
}
// 自定义排序
function sortChange(column) {
	var sort = undefined
	var sortType = undefined

	if (column.prop != null && column.order != null) {
		sort = column.prop
		sortType = column.order

	}
	queryParams.sort = sort
	queryParams.sortType = sortType
	handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
	single: true,
	multiple: true,
	form: {},
	rules: {
		bomId: [{ required: true, message: "bom主键不能为空", trigger: "blur" }],
		itemNo: [{ required: true, message: "产品编号不能为空", trigger: "blur" }],
		itemType: [{ required: true, message: "产品类型不能为空", trigger: "change" }],
		partCode: [{ required: true, message: "零件号不能为空", trigger: "blur" }],
		qty: [{ required: true, message: "用量不能为空", trigger: "blur" }],
		isFill: [{ required: true, message: "是否填充(0=否,1=是)不能为空", trigger: "change" }],
	},
	options: {
		// 产品类型 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
		product_type: [],
		// 是否填充(0=否,1=是) 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
		bom_is_fill: [],
		// 获取类型（E：自制件；F：外购件） 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
		bom_obtain_type: [],
	}
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
	open.value = false
	reset()
}

// 重置表单
function reset() {
	form.value = {
		bomId: null,
		itemNo: null,
		itemType: null,
		partCode: null,
		qty: null,
		effectDate: null,
		expirationDate: null,
		changeNo: null,
		supplierCode: null,
		wareCode: null,
		isFill: null,
		dmsOrderNo: null,
		planNo: null,
		obtainType: null,
		createUser: null,
		createBy: null,
		createTime: null,
		updateBy: null,
		updateTime: null,
		remark: null,
	};
	proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
	reset();
	open.value = true
	title.value = '添加bom信息'
	opertype.value = 1
	form.value.bomId = proxy.parseGuid();
}
// 修改按钮操作
function handleUpdate(row) {
	reset()
	const id = row.bomId || ids.value
	getProdBom(id).then((res) => {
		const { code, data } = res
		if (code == 200) {
			open.value = true
			title.value = '修改bom信息'
			opertype.value = 2

			form.value = {
				...data,
			}
		}
	})
}

// 添加&修改 表单提交
function submitForm() {
	proxy.$refs["formRef"].validate((valid) => {
		if (valid) {

			if (form.value.bomId != undefined && opertype.value === 2) {
				updateProdBom(form.value).then((res) => {
					proxy.$modal.msgSuccess("修改成功")
					open.value = false
					getList()
				})
			} else {
				addProdBom(form.value).then((res) => {
					proxy.$modal.msgSuccess("新增成功")
					open.value = false
					getList()
				})
			}
		}
	})
}

// 删除按钮操作
function handleDelete(row) {
	const Ids = row.bomId ? [row.bomId] : undefined || ids.value
	proxy
		.$confirm('是否确认删除"' + Ids.length + '"条数据项？', "警告", {
			confirmButtonText: proxy.$t('common.ok'),
			cancelButtonText: proxy.$t('common.cancel'),
			type: "warning",
		})
		.then(function () {
			return delProdBom(Ids)
		})
		.then(() => {
			getList()
			proxy.$modal.msgSuccess("删除成功")
		})
}


// 导入数据成功处理
const handleFileSuccess = (response) => {
	const { item1, item2 } = response.data
	var error = ''
	item2.forEach((item) => {
		error += item.storageMessage + ','
	})
	proxy.$alert(item1 + '<p>' + error + '</p>', '导入结果', {
		dangerouslyUseHTMLString: true
	})
	getList()
}

// 导出按钮操作
function handleExport() {
	proxy
		.$confirm("是否确认导出bom信息数据项?", "警告", {
			confirmButtonText: "确定",
			cancelButtonText: "取消",
			type: "warning",
		})
		.then(async () => {
			await proxy.downFile('/ProdManage/ProdBom/export', { ...queryParams })
		})
}

handleQuery()
</script>